package com.bwie.record.utils;

import org.apache.commons.codec.binary.Base64;
import org.springframework.util.ResourceUtils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Base64ToImage {

    public static void convertBase64ToImage(String base64Image, String outputPath) throws IOException {
        // 解码Base64编码的字符串
        byte[] imageBytes = Base64.decodeBase64(base64Image);

        // 将字节数组转换为BufferedImage对象
        ByteArrayInputStream bais = new ByteArrayInputStream(imageBytes);
        BufferedImage bufferedImage = ImageIO.read(bais);

        // 将BufferedImage对象保存为图片文件
        ImageIO.write(bufferedImage, "jpg", new File(outputPath));
    }

    public static void main(String[] args) {
        try {
            // 替换为你的Base64编码的图片字符串和输出路径
            String base64Image = "your_base64_encoded_image_string";
            String outputPath = "path/to/save/image.jpg";
            convertBase64ToImage(base64Image, outputPath);
            System.out.println("图片已保存到：" + outputPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
